import { BannersEntity } from './../../entity/banners.entity';
import { PaginationQueryDto } from './../../common/dto/paginationQuery.dto';
import { UpdateBannerDto } from './dto/updateBanner.dto';
import { CreateBannerDto } from './dto/createBanner.dto';
import { BannersService } from './banners.service';
import { Body, Controller, Get, Header, Param, Post } from '@nestjs/common';
import { toDataUTCTime, toSimpleTime } from '../../utils/timeUtil';
import { LessThan, MoreThan } from 'typeorm';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { NotAuthorized } from 'src/decorator/roles.decorator';

@ApiTags('轮播')
@Controller('banners')
export class BannersController {
    constructor(private readonly bannersServ: BannersService) { }

    @Header('Cache-Control','max-age=1000')
    @Header('Cache-Control','public')
    @NotAuthorized()
    @ApiOperation({ summary: '轮播列表' })
    @Post()
    index(@Param() params: PaginationQueryDto): any {
        const date = toSimpleTime(new Date());
        return this.bannersServ.pagination(params, {
            where: {
                startAt: LessThan(date),
                endAt: MoreThan(date),
                status: 1
            },
            order: {
                sort: 'DESC',
                updateTime: 'DESC'
            }
        })
    }

    @ApiOperation({ summary: '添加轮播' })
    @Post('add')
    async addBanner(@Body() createDto: CreateBannerDto): Promise<any> {
        createDto.startAt = toDataUTCTime(createDto.startAt);
        createDto.endAt = toDataUTCTime(createDto.endAt);
        await this.bannersServ.createOne(createDto);
        return { message: '添加成功！' }
    }

    @ApiOperation({ summary: '更新轮播' })
    @Post('update')
    async putUpdate(@Body() dto: UpdateBannerDto): Promise<any> {
        console.log(new UpdateBannerDto())
        await this.bannersServ.updateById(dto.id, dto, { origin: new UpdateBannerDto() });
        return { message: '更新成功！' };
    }

    @ApiOperation({ description: '删除轮播' })
    @Post('delete')
    async del(@Body('id') id: string): Promise<any> {
        await this.bannersServ.deleteById(id);
        return { message: '删除成功！' };
    }

}
